<?xml version="1.0"?>
<!--
lib_axis.xsl
$Revision: 4 $
Contains named templates that outputs axis

Created by: Virgil Dupras
Created on: 2004/09/10

Last modified by:$Author: hardcoded.software $
Last modified on:$Date: 2006-08-26 14:05:46 +0000 (Sat, 26 Aug 2006) $

Copyright (C) 2004-2006 Virgil Dupras (http://www.hardcoded.net)

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/2000/svg" exclude-result-prefixes="#default">
                
<xsl:output 
	method="xml"
	doctype-public="-//W3C//DTD SVG 1.0//EN"
	doctype-system="http://www.w3.org/TR/SVG/DTD/svg10.dtd"
	indent="yes"/>
	
<xsl:template match="xaxis">
	<g id="x_axis">
		<xsl:if test="../yaxis/margin">
			<xsl:attribute name="transform">translate(<xsl:value-of select="../yaxis/margin"/>,0)</xsl:attribute>
		</xsl:if>
		<line class="histframe" x1="50" x2="550" y1="350" y2="350">
			<xsl:if test="../yaxis/margin">
				<xsl:attribute name="x2"><xsl:value-of select="550 - ../yaxis/margin"/></xsl:attribute>
			</xsl:if>
		</line>
		<text class="axistitle" x="300" y="395">
			<xsl:value-of select="title"/>
		</text>
		<xsl:apply-templates select="grid"/>
	</g>
</xsl:template>

<xsl:template match="yaxis">
	<text class="axistitle" writing-mode="tb" glyph-orientation-vertical="0" x="10" y="200">
		<xsl:value-of select="title"/>
	</text>
	<g id="y_axis">
		<xsl:if test="margin">
			<xsl:attribute name="transform">translate(<xsl:value-of select="margin"/>,0)</xsl:attribute>
		</xsl:if>
		<line class="histframe" x1="50" x2="50" y1="50" y2="350"/>
		<xsl:apply-templates select="grid"/>
	</g>
</xsl:template>

<!-- NAMED TEMPLATES -->

<xsl:template name="draw_x_grid">
	<xsl:param name="pos_method" select="0"/> <!-- 0 = The line goes in the middle of the space allowed for it. 1 = The line goes at the start of that space -->
    <xsl:variable name="axis_width">
    	<xsl:if test="../../yaxis/margin"><xsl:value-of select="500 - ../../yaxis/margin"/></xsl:if>
    	<xsl:if test="not(../../yaxis/margin)">500</xsl:if>
    </xsl:variable>
	<xsl:variable name="gridcount" select="count(line)"/>
    <xsl:variable name="mywidth">
		<xsl:if test="$pos_method = 0"><xsl:value-of select="$axis_width div $gridcount"/></xsl:if>
		<xsl:if test="$pos_method = 1"><xsl:value-of select="$axis_width div ($gridcount - 1)"/></xsl:if>	
	</xsl:variable>
	<xsl:for-each select="line">
		<xsl:variable name="lineno" select="position() - 1"/>
		<xsl:variable name="xpos_tmp">
			<xsl:choose>
				<xsl:when test="@value"><xsl:value-of select="50 + ((@value div ../../max) * $axis_width)"/></xsl:when>
				<xsl:when test="../valuepositioning"><xsl:value-of select="50 + ((number() div ../../max) * $axis_width)"/></xsl:when>
				<xsl:otherwise><xsl:value-of select="50 + ($lineno * $mywidth)"/></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="xpos">
			<xsl:if test="$pos_method = 0"><xsl:value-of select="$xpos_tmp + ($mywidth div 2)"/></xsl:if>
			<xsl:if test="$pos_method = 1"><xsl:value-of select="$xpos_tmp"/></xsl:if>	
		</xsl:variable>
		<line class="gridline" x1="{$xpos}" x2="{$xpos}" y1="350" y2="360"/>
		<text class="xgrid" x="{$xpos}" y="370">
			<xsl:if test="((../longstrfit) and ((position() mod 2) = 0)) or (@lowered)">
				<xsl:attribute name="y">382</xsl:attribute>
			</xsl:if>
			<xsl:value-of select="."/>
		</text>
	</xsl:for-each>
</xsl:template>

<xsl:template name="draw_y_grid">
	    <xsl:variable name="gridcount" select="count(line)"/>
	    <xsl:variable name="mywidth" select="300 div $gridcount"/>
	    <xsl:variable name="indicator_width" select="500 - ../margin"/>
	    <xsl:variable name="ymax" select="../max"/>
	    <xsl:variable name="ymin" select="sum(../min)"/> <!-- A hack to make the script work even if there is no min element -->
	    <xsl:variable name="ydelta" select="$ymax - $ymin"/>
	    <xsl:for-each select="line">
			<xsl:variable name="lineno" select="position()"/>
			<xsl:variable name="ypos">
				<xsl:choose>
					<xsl:when test="@value"><xsl:value-of select="350 - (((@value - $ymin) div $ydelta) * 300)"/></xsl:when>
					<xsl:when test="../valuepositioning"><xsl:value-of select="350 - (((number() - $ymin) div $ydelta) * 300)"/></xsl:when>
					<xsl:otherwise><xsl:value-of select="300 - ($lineno * $mywidth)"/></xsl:otherwise>
				</xsl:choose>
			</xsl:variable>
			<line class="gridline" x1="40" x2="50" y1="{$ypos}" y2="{$ypos}"/>
			<text class="ygrid" x="39" y="{($ypos+4)}"><xsl:value-of select="."/></text>
			<xsl:if test="@indicator">
				<line class="gridline" style="stroke:{@indicator}" x1="50" x2="{50 + $indicator_width}" y1="{$ypos}" y2="{$ypos}"/>	
			</xsl:if>
		</xsl:for-each>
</xsl:template>

</xsl:stylesheet>